// 引入接口文件
import {getType,getMonthData} from './statisticsViewServ.js'

// 对象和JSON字符串互转
import { parse } from 'qs'
// 弹出消息框
import { message } from 'antd'
// 日期处理对象
import moment from 'moment'
/* isEmpty 对象是否为空
 * isArray 对象是否数组
 * cloneDeep 对象深拷贝
 */
import {isEmpty, isArray, cloneDeep, merge} from 'lodash'
// 引入路由对象
import { hashHistory } from 'dva/router'
import { makeAddress } from "utils/util"
// 初始默认状态
const defultState = {
  loading: false,
  btnLoading: false,
  
  noTitleKey:'Overview',

  tableQuery: {
		startDate: moment().subtract(1,"months").format('YYYY-MM-DD'),
		endDate: moment().format('YYYY-MM-DD')
	},

}
// 声明module
const tmpModule = {
  namespace: 'statisticsViewModel',
  state: cloneDeep(defultState),
  subscriptions: {
    setup ({dispatch, history}) {
      // console.log(history)
      history.listen(({ pathname, query }) => {
        console.log(location)
        if('' + tmpModule.pathname === '' + pathname){
          dispatch({ type: 'updateStore', payload: cloneDeep(defultState) });
          dispatch({ type: 'typeList', payload: {} });
        }
      })
    },
  },
  // 方法列表
  effects: {
    // *typeList({payload},{ put, call, select }){
    //   try{
    //     const { rows, success } = yield call(getType);
    //     let defultState = yield select(state => state[tmpModule.namespace])
    //     if(success) {
    //       yield put({ type: 'updateStore',payload:{typeOption:rows}})
    //     }else{
    //     }
    //   }catch(e){

    //   }
    // },
    *changeTab({payload},{ put, call, select }){
      console.log(payload)
      yield put({ type: 'updateStore',payload:{noTitleKey:payload.key}})
    },
    *searchData({ payload }, { put, call, select }) {
			let state = yield select(state => state[tmpModule.namespace])
			yield put({
				type: 'getMonthData',
				payload: {
					params: {
						// couponsConfigId: state.couponsConfigId,
						...state.tableQuery
					}
				}
			})
		},
    
  },
  
 

  // 存入状态机
  reducers: {
    updateStore(preState, action) {
      return { ...preState, ...action.payload };
    }
  }
}

export default tmpModule
